﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar.Controls;
using System.Drawing;
using DevComponents.DotNetBar;
using System.Data;
namespace CDStore3Tiers
{
    public class XuLyDataGridView
    {
        public static DataGridViewComboBoxColumn CotCombo(string TenCot, object Datasource, string display, string value)
        {
            DataGridViewComboBoxColumn cl = CotCombo(TenCot, Datasource);

            try
            {
                cl.DisplayMember = display;
                cl.ValueMember = value;
            }
            catch (Exception)
            {
            }
            return cl;
        }
        public static DataGridViewComboBoxColumn CotCombo(string TenCot, object Datasource, string display, string value, string propertyname)
        {
            DataGridViewComboBoxColumn cl = CotCombo(TenCot, Datasource, display, value);
            cl.DataPropertyName = propertyname;
            return cl;
        }
        public static DataGridViewComboBoxColumn CotCombo(string TenCot, object Datasource)
        {
            DataGridViewComboBoxColumn cl = new DataGridViewComboBoxColumn();
            cl.HeaderText = TenCot;
            cl.DataSource = Datasource;
            return cl;
        }
        public static DataGridViewButtonColumn CotButton(string TenCot)
        {
            DataGridViewButtonXColumn cot = new DataGridViewButtonXColumn();
            cot.HeaderText = cot.Text = TenCot;
            cot.UseColumnTextForButtonValue = true;
            cot.ToolTipText = "Nhấn để " + TenCot + " hàng này";
            return cot;
        }

        public static void CotCheckBox(DataGridView dgr)
        {
            DataGridViewCheckBoxXColumn cot = new DataGridViewCheckBoxXColumn();
            cot.HeaderText = "+";
            cot.Checked = false;
            cot.Width = 20;
            cot.Checked = false;
            dgr.Columns.Insert(0, cot);
        }

        public static void CheckRow(DataGridView dgr, List<int> DanhSach, DataGridViewCellMouseEventArgs e)
        {
            try
            {

                if (e.ColumnIndex == 0 && e.RowIndex == -1)
                {
                    bool chon = (DanhSach.Count == 0);
                    if (chon)
                        foreach (DataGridViewRow r in dgr.Rows)
                        {
                            r.Cells[0].Value = chon;
                            DanhSach.Add(r.Index);
                        }
                    else
                    {
                        foreach (int i in DanhSach)
                            dgr.Rows[i].Cells[0].Value = false;
                        DanhSach.Clear();
                    }
                    return;
                }
                else
                {
                    bool vl = Convert.ToBoolean(dgr.CurrentRow.Cells[0].Value);
                    dgr.CurrentRow.Cells[0].Value = !vl;
                    if (vl)
                        DanhSach.Remove(e.RowIndex);
                    else
                        DanhSach.Add(e.RowIndex);
                }
            }
            catch (Exception)
            {
            }

        }

        public static DataGridViewButtonColumn CotButton(string TenCot, int width)
        {
            DataGridViewButtonXColumn cot = new DataGridViewButtonXColumn();
            cot.HeaderText = cot.Text = TenCot;
            cot.UseColumnTextForButtonValue = true;
            cot.Width = width;
            cot.ToolTipText = "Nhấn để " + TenCot + " hàng này";
            return cot;
        }
        public static void SetDataSource(DataGridViewComboBoxColumn cl, DataSet ds, string display, string value)
        {
            cl.DataSource = ds.Tables[0];
            cl.DisplayMember = display;
            cl.ValueMember = value;
        }
        public static bool AddCotButtom(DataGridView dgr, string Text)
        {
            try
            {
                dgr.Columns.Add(CotButton(Text));
                return true;
            }
            catch (Exception)
            {
            }
            return false;
        }
        public static bool AddCotButtom(DataGridView dgr, string Text, int width)
        {
            try
            {
                dgr.Columns.Insert(0, CotButton(Text, 10));
                return true;
            }
            catch (Exception)
            {
            }
            return false;
        }


        public static object GetObject(DataGridViewRow r, String Name)
        {
            try
            {
                return r.Cells[Name].Value;
            }
            catch (Exception)
            {
                return "";
            }
        }
        public static String GetString(DataGridViewRow r, String Name)
        {
            try
            {
                return r.Cells[Name].Value.ToString();
            }
            catch (Exception)
            {
                return "";
            }
        }
        public static DateTime GetDate(DataGridViewRow r, String Name)
        {
            return ConvertType.ToDate(GetObject(r, Name));
        }
        public static int GetInt(DataGridViewRow r, String Name)
        {
            return ConvertType.ToInt(GetObject(r, Name));
        }





        public static void dgrMouseMove(DataGridView dgr)
        {
            dgr.CellMouseMove += new DataGridViewCellMouseEventHandler(dgr_CellMouseMove);
            dgr.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        static void dgr_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                DataGridView d = sender as DataGridView;
                d.CurrentCell = d.Rows[e.RowIndex].Cells[e.ColumnIndex];
                d.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true;
            }
            catch (Exception)
            {
            }
        }

        public static void Style(DataGridViewX dgr)
        {
            //dgr.AllowUserToAddRows = false;
            //dgr.AllowUserToDeleteRows = false;
            //dgr.AllowUserToResizeColumns = false;
            //dgr.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            //dgr.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
            //dgr.ColumnHeadersHeight = 40;
            //dgr.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            //dgr.ReadOnly = true;
            //dgr.RowHeadersVisible = false;
            //dgr.RowHeadersWidth = 50;
            //dgr.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            //dgr.RowTemplate.Height = 40;
            //dgr.RowTemplate.Resizable = System.Windows.Forms.DataGridViewTriState.False;
            //dgr.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            //DataGridViewCellStyle dataGridViewCellStyle1 = new DataGridViewCellStyle();
            //dataGridViewCellStyle1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(207)))), ((int)(((byte)(221)))), ((int)(((byte)(238)))));
            //dataGridViewCellStyle1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192)))));
            //dataGridViewCellStyle1.SelectionBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
            //dataGridViewCellStyle1.SelectionForeColor = System.Drawing.Color.Blue;
            //dgr.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
            //DataGridViewCellStyle dataGridViewCellStyle2 = new DataGridViewCellStyle();
            //dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
            //dataGridViewCellStyle2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(207)))), ((int)(((byte)(221)))), ((int)(((byte)(238)))));
            //dataGridViewCellStyle2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(163)));
            //dataGridViewCellStyle2.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192)))));
            //dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
            //dataGridViewCellStyle2.SelectionForeColor = System.Drawing.Color.Blue;
            //dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
            //dgr.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle2;
            //DataGridViewCellStyle dataGridViewCellStyle3 = new DataGridViewCellStyle();
            //dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
            //dataGridViewCellStyle3.BackColor = System.Drawing.SystemColors.Window;
            //dataGridViewCellStyle3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(163)));
            //dataGridViewCellStyle3.ForeColor = System.Drawing.Color.Black;
            //dataGridViewCellStyle3.SelectionBackColor = System.Drawing.SystemColors.Highlight;
            //dataGridViewCellStyle3.SelectionForeColor = System.Drawing.Color.Blue;
            //dataGridViewCellStyle3.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
            //dgr.DefaultCellStyle = dataGridViewCellStyle3;
            //dgr.GridColor = System.Drawing.Color.FromArgb(((int)(((byte)(170)))), ((int)(((byte)(170)))), ((int)(((byte)(170)))));
            //DataGridViewCellStyle dataGridViewCellStyle4 = new DataGridViewCellStyle();
            //dataGridViewCellStyle4.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
            //dataGridViewCellStyle4.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
            //dataGridViewCellStyle4.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(163)));
            //dataGridViewCellStyle4.ForeColor = System.Drawing.Color.Blue;
            //dataGridViewCellStyle4.SelectionBackColor = System.Drawing.SystemColors.Highlight;
            //dataGridViewCellStyle4.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
            //dataGridViewCellStyle4.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
            //dgr.RowHeadersDefaultCellStyle = dataGridViewCellStyle4;
            //DataGridViewCellStyle dataGridViewCellStyle5 = new DataGridViewCellStyle();
            //dataGridViewCellStyle5.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
            //dataGridViewCellStyle5.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192)))));
            //dataGridViewCellStyle5.SelectionBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))));
            //dataGridViewCellStyle5.SelectionForeColor = System.Drawing.Color.Blue;
            //dgr.RowsDefaultCellStyle = dataGridViewCellStyle5;
            //dgr.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
        }
        public static void Style(DataGridViewX[] listdgr)
        {
            foreach (DataGridViewX dgr in listdgr)
                Style(dgr);
        }

        public static void Style3(DataGridViewX dgr)
        {
            Style(dgr);
            dgr.ReadOnly = false;
            dgr.AllowUserToAddRows = true;
            dgr.AllowUserToDeleteRows = true;
        }

        public static void Style2(DataGridViewX dgr)
        {
            dgr.AllowUserToAddRows = false;
            dgr.AllowUserToDeleteRows = false;
            dgr.AllowUserToResizeColumns = false;
            dgr.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            dgr.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
            dgr.ColumnHeadersHeight = 40;
            dgr.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            dgr.ReadOnly = true;
            dgr.RowHeadersVisible = false;
            dgr.RowHeadersWidth = 50;
            dgr.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            dgr.RowTemplate.Height = 40;
            dgr.RowTemplate.Resizable = System.Windows.Forms.DataGridViewTriState.False;
            dgr.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
        }
        public static void Style2(DataGridViewX dgr, int height)
        {
            dgr.AllowUserToAddRows = false;
            dgr.AllowUserToDeleteRows = false;
            dgr.AllowUserToResizeColumns = false;
            dgr.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            dgr.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
            dgr.ColumnHeadersHeight = 40;
            dgr.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            dgr.ReadOnly = true;
            dgr.RowHeadersVisible = false;
            dgr.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            dgr.RowTemplate.Height = height;
            dgr.RowTemplate.Resizable = System.Windows.Forms.DataGridViewTriState.False;
            dgr.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
        }


    }
}
